﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Data;
using System.Data.SQLite;

namespace CPUdata
{
  public  class cpuD
  {
    //variable for the connection
     private static SQLiteConnection sql_con;

     public static void SetConnection()
      {
         // open the database
          String connectionString = "data source=" + "C:\\Mosaic\\powerdb" +
        ";Version=3;";
          sql_con = new SQLiteConnection
              (connectionString);
      } 


 //the main body of the program
   public static void cpuFunction(int id1)
     {
       //tries to open the database , if it can't prints out a error message
         SetConnection();
         try
         {
            
            sql_con.Open();

         }

         catch (Exception)
         {

             Console.WriteLine("not open");
         }

         //get the process for the workbench 
         Process[] pr1 = Process.GetProcessesByName("nb3");//processname here
         string cpu = string.Empty;
         string info = string.Empty;
         PerformanceCounter pp = new PerformanceCounter();
         pp.CategoryName = "Process";
         pp.CounterName = "% Processor Time";
         pp.InstanceName = "nb3";//processname here
         pp.MachineName = ".";
         Process pr = pr1[0];
         string pname = pr.ProcessName;
       
            
                 
                     string cpuusage = Math.Round(pp.NextValue(), 2).ToString();
                     string memusage = (Convert.ToInt64(pr.WorkingSet64.ToString()) / 1024).ToString();

                     DateTime tTime = DateTime.Now;
                     string info2 = pr.ProcessName + "Memory：" +
                                             (Convert.ToInt64(pr.WorkingSet64.ToString()) / 1024).ToString();
                     Console.WriteLine(info2 + "    CPU usage：" + Math.Round(pp.NextValue(), 2).ToString() + "%");

                     //query search for process name if not there add
                     SQLiteCommand cmd = new SQLiteCommand("  SELECT ProcessID, ProcessName FROM Process WHERE  (ProcessName = '" + pname + "')", sql_con);

                     // Use the above SqlCommand object to create a SqlDataReader object.
                     SQLiteDataReader queryCommandReader = cmd.ExecuteReader();

                     // Create a DataTable object to hold all the data returned by the query.
                     DataTable dataTable = new DataTable();

                     // Use the DataTable.Load(SqlDataReader) function to put the results of the query into a DataTable.
                     dataTable.Load(queryCommandReader);
                     int check = dataTable.Rows.Count;
              
                     if (check == 0)
                     {
                         try
                         {
                             cmd = new SQLiteCommand("insert into Process (ProcessName) values ('" + pname + "')", sql_con);
                             cmd.ExecuteNonQuery();

                             cmd = new SQLiteCommand("  SELECT ProcessID, ProcessName FROM Process WHERE  (ProcessName = '" + pname + "')", sql_con);

                             // Use the above SqlCommand object to create a SqlDataReader object.
                             queryCommandReader = cmd.ExecuteReader();
                             Console.WriteLine("Stored");
                             // Create a DataTable object to hold all the data returned by the query.
                             dataTable = new DataTable();

                             // Use the DataTable.Load(SqlDataReader) function to put the results of the query into a DataTable.
                             dataTable.Load(queryCommandReader);
                         }
                         catch
                         {

                             Console.WriteLine("does not work");
                             }

                     }

                         //its already there add the data to the datatable
                         Int64  id2 = (Int64)dataTable.Rows[0][0];
                         //insert row into data table with this id
                         cmd = new SQLiteCommand("insert into  taskManager(DataID,Memory,CPU) values ('" + id2 + "','"  + memusage + "','" + cpuusage + "' )", sql_con);
                         cmd.ExecuteNonQuery();
                       //  Console.WriteLine("Stored");
                       


                     }
                 



             
         
     
        

       



      
    }
}